package com.itxx.udp.Unicast.udpDemo1;

import java.io.IOException;
import java.net.DatagramPacket;
import java.net.DatagramSocket;

public class Receive1 {
    public static void main(String[] args) throws IOException {
        //1.创建接受端对象  DatagramSocket(int port)  从哪个端口接受数据
        DatagramSocket ds = new DatagramSocket(54321);
        //创建datagramPOcket 对象用于接收传过来的数据
        byte[] bytes = new byte[1024];
        DatagramPacket dp = new DatagramPacket(bytes, bytes.length);
        //接收数据将数据放入DatagramPacket中
        ds.receive(dp);
        //从DatagramPacket中拿走写数据
        byte[] data = dp.getData();
        //接收数据的实际长度
        int length = dp.getLength();
        String s = new String(data, 0, length);
        System.out.println("接受的数据是:" + s);
    }
}
